package priv.liaohaolong.fullstacktest.service.impl;

import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    /**
     * 按顺序可以抛出以下异常（约定）：
     * <ol>
     *     <li>DisabledException: 用户被禁用</li>
     *     <li>LockedException: 用户被锁定</li>
     *     <li>BadCredentialsException: 用户名或密码错误</li>
     * </ol>
     *
     * @param username the username identifying the user whose data is required.
     * @return the UserDetails requested
     * @throws BadCredentialsException 用户名或密码错误
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws BadCredentialsException {
        if ("admin".equals(username)) {
            // 用户名：admin，密码：123456789
            return User.withUsername("admin").password("$2a$10$4BeTWpp91SltQo3wZT8mhuA4gtw0DT9Hne1uh/ouyAxobQfNoCEGK").build();
        }
        throw new BadCredentialsException("User not found");
    }

}
